<?php
declare (strict_types = 1);

namespace app\controller;


use app\service\EmailService;
use think\Exception;
use think\facade\View;

class AdminEmail extends AdminInit
{
    public function __construct()
    {
        parent::__construct();
    }
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        View::assign("title","邮件配置");
        return  view();
    }

    public function save()
    {
        if(request()->isPost()){
            $arr = input("post.");
            //将数组写入配置文件
            $a = var_export($arr,true);
            $res = file_put_contents("./config/email.php","<?php \n return ".$a.';');

            if($res){
                return  json(["code"=>200,"msg"=>"成功"]);
            }else{
                return  json(["code"=>4000,"msg"=>"请稍后再试"]);
            }
        }
        View::assign("title","文章列表");
        return  view();
    }

    //发送测试邮件
    public function test()
    {
        $email = input("param.email");
        try{
            $res = EmailService::send($email);
            if($res){
                return  json(["code"=>200,"msg"=>"邮件发送成功"]);
            }else{
                return  json(["code"=>4000,"msg"=>"请稍后再试"]);
            }
        }catch (Exception $exception){
            return  json(["code"=>4000,"msg"=>$exception->getMessage()]);
        }
    }




}
